//
//  MyContactsViewModel.m
//  绿地管理系统
//
//  Created by rimi on 16/1/25.
//  Copyright © 2016年 hexs. All rights reserved.
//

#import "MyContactsViewModel.h"
#import "MyContactsModel.h"

@interface MyContactsViewModel ()

@property (nonatomic, strong) MyContactsModel *model;

@end

@implementation MyContactsViewModel

- (void)getData
{
    [self.model getdata];
}

- (NSString *)observingKeyPath
{
    return @"model.myPeopleList";
}

- (BOOL)isSuccess
{
    return ![self.model.myPeopleList isKindOfClass:[NSNull class]];
}
// 私有方法
- (NSDictionary *)cellInfoDicForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.model.myPeopleList[@"content"][@"people_list"][indexPath.row];
}

// viewModel来确定tableView的某个section下面有多少行cell
- (NSInteger)numberOfRowsInSection:(NSInteger)section
{
    return [self.model.myPeopleList[@"content"][@"people_list"] count];
}

// 指定的indexPath下面的cell上面的名字
- (NSString *)cellNameForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *cellInfo = [self cellInfoDicForRowAtIndexPath:indexPath];
    return cellInfo[@"real_name"];
}


- (NSString *)cellPhotoUrlForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *cellInfo = [self cellInfoDicForRowAtIndexPath:indexPath];
    return cellInfo[@"avatar"];
}

- (id)cellGenderForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *cellInfo = [self cellInfoDicForRowAtIndexPath:indexPath];
    return cellInfo[@"sex"];
}
- (NSArray *)cellRelationListForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *cellInfo = [self cellInfoDicForRowAtIndexPath:indexPath];
    return cellInfo[@"relate_other_people_list"];
}

//- (NSArray *)expandCellRelationListForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//
//}


- (CGFloat)heightForCellAtIndexPath:(NSIndexPath *)indexPath isExpand:(BOOL)isExpand
{
    // 高度不会变化的地方+高度会变化的地方
    // 除开会变的高度，剩下的高度都是不会变的，所以用一个写死的数来指定
    CGFloat fixHieght = 38+25;
    NSArray * cellDataSource = [self cellRelationListForRowAtIndexPath:indexPath];
    CGFloat dynamicHeight ;
    
    if (isExpand) {
        dynamicHeight = cellDataSource.count * 30;
    } else {
        // 收起，先看你有多少条
        NSInteger count = cellDataSource.count;
        // 如果没有2条，有多少条就显示多少条
        if (count < 2) {
            dynamicHeight = count * 30;
        } else {
            // 如果有两条以上，就只显示两条
            dynamicHeight = 2 * 30;
        }
        
    }
    
    return fixHieght + dynamicHeight;
}



#pragma mark -- getter
- (MyContactsModel *)model
{
    if (!_model) {
        _model = [[MyContactsModel alloc] init];
    }
    return _model;
}
@end












